package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import struts.form.FullForm;
import struts.form.XmlBean;

public class ActionServlet extends HttpServlet {
	/**
	 * 
	 */	
	public void doGet(HttpServletRequest request,HttpServletResponse response)
	throws IOException,ServletException
	{
		request.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String path = this.getPath(request.getServletPath());
		Map<String,XmlBean> map = (Map<String, XmlBean>) this.getServletContext().getAttribute("struts");
		XmlBean xml = map.get(path);
		String formClass = xml.getFormClass();
		ActionForm form = (ActionForm)FullForm.full(formClass, request);
		String actionType=xml.getActionType();
		
		Action action=null;
		String url = "";
		try{
			Class clazz = Class.forName(actionType);
			action = (Action) clazz.newInstance();
			url=action.execute(request, form, xml.getActionForward());
		}catch(Exception e){
			e.printStackTrace();
			System.out.println( "严重：控制器错误错误!" );
		}
	
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		dispatcher.forward(request, response);

	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response)
	throws IOException,ServletException
	{
		this.doGet(request, response);
	}
	
	public String getPath(String servletpath)
	{
		return servletpath.split("\\.")[0];
	}
}
